﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfleet.SE.UICompiler.Core
{
    /// <summary>
    /// Represents the result of a compilation.
    /// </summary>
    public class CompileResult
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="files">Paths of the input files to compilation</param>
        /// <param name="compiledFilePath">Path of the output file</param>
        public CompileResult(IEnumerable<string> files, string compiledFilePath)
            : this(files, compiledFilePath, new FileProvider())
        { }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="files">Paths of the input files to compilation</param>
        /// <param name="compiledFilePath">Path of the output file</param>
        /// <param name="fileSizeProvider">An instance of <see cref="IFileSizeProvider"/></param>
        /// <exception cref="ArgumentNullException">If <paramref name="fileSizeProvider"/> or <paramref name="files"/>
        /// is null.</exception>
        /// <seealso cref="IFileProvider"/>
        public CompileResult(IEnumerable<string> files, string compiledFilePath, IFileProvider fileSizeProvider)
        {
            if (fileSizeProvider == null)
            {
                throw new ArgumentNullException("fileSizeProvider", "value cannot be null");
            }
            if(files == null)
            {
                throw new ArgumentNullException("files", "value cannot be null");
            }
            _fileSizeProvider = fileSizeProvider;
            _files = files;
            _compiledFilePath = compiledFilePath;
        }

        private IFileProvider _fileSizeProvider;
        private IEnumerable<string> _files;
        private string _compiledFilePath;

        /// <summary>
        /// True if compilation succeeded
        /// </summary>
        public bool Success
        {
            get;
            set;
        }

        /// <summary>
        /// Status message from the compiler
        /// </summary>
        public string StatusMessage
        {
            get;
            set;
        }

        /// <summary>
        /// Total initial size of all input files
        /// </summary>
        public long InitialFileSize
        {
            get
            {
                long fileSize = 0L;
                foreach(var file in _files)
                {
                    fileSize += _fileSizeProvider.GetFileSize(file);
                }
                return fileSize;
            }
        }

        /// <summary>
        /// Size in bytes of the compiled file
        /// </summary>
        public long CompiledFileSize
        {
            get
            {
                return _fileSizeProvider.GetFileSize(_compiledFilePath);
            }
        }

        /// <summary>
        /// Compress factor (initial size contra size of compiled file)
        /// </summary>
        public float CompressFactor
        {
           get 
           {
               if (CompiledFileSize == 0L || InitialFileSize == 0L)
               {
                   return 0L;
               }
               return (float)CompiledFileSize / (float)InitialFileSize; 
           }
        }
    }
}
